import { Injectable, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Injectable()
export class BackMenuService implements OnInit{
  private url:string = '';
  private backMenus:any = [];
  private menus:any = [];

  constructor(private router:Router){
  }

  ngOnInit(){
  }

  public getBackMenu(val:string):any{
    this.menus = JSON.parse(localStorage.getItem("c_menus"));
    if(this.url != val && this.menus){
      this.backMenus = [];
      this.url = val;

      let a = this.url.split('/');
      a.splice(0,2);
      let name = '';
      let url = '';
      for(let i = 0 ; i < a.length ; i ++){
        name = '';
        url = '';
        if(a[i] == 'type' || a[i] == 'layer'){
          name = this.menus.head[a[i]+a[i+1]] ;
          url = this.url.split('/' + a[i]+'/'+a[i+1])[0];
          i++;
          if(this.menus.head[name] != null && a[i] == 'type'){
            continue;
          }
        }
        else{
          if(!this.menus.head[a[i]]) {
            continue;
          }
          name = this.menus.head[a[i]];
          url = this.url.split('/' + a[i])[0];
        }
        this.backMenus.splice(this.backMenus.length,0,{name:name,url:url});
      }
    }


    return this.backMenus;
  }

}
